package cn.jy.careuplusdoctor.utils;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import cn.jy.careuplusdoctor.utils.ProgressMultipartEntity.ProgressCallback;

public abstract class BaseMultipartManager<T>  extends AsyncTask<String, Void, String> {
	
	protected Callback<T> callback;
	protected List<BasicNameValuePair> params;
	protected List<Param> multipartParams;
	protected String url;
	protected boolean isMultipart = false;
	protected ProgressCallback progressCallback = null;

	public List<Param> getMultipartParams() {
		return multipartParams;
	}

	public void setMultipartParams(List<Param> multipartParams) {
		this.multipartParams = multipartParams;
	}
	
	public boolean isMultipart() {
		return isMultipart;
	}

	public void setMultipart(boolean isMultipart) {
		this.isMultipart = isMultipart;
	}

	public Callback<T> getCallback() {
		return callback;
	}

	public void setCallback(Callback<T> callback) {
		this.callback = callback;
	}

	public List<BasicNameValuePair> getParams() {
		return params;
	}

	public void setParams(List<BasicNameValuePair> params) {
		this.params = params;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public ProgressCallback getProgressCallback() {
		return progressCallback;
	}

	public void setProgressCallback(ProgressCallback progressCallback) {
		this.progressCallback = progressCallback;
	}
	
	/**
	 * 数据字段：状态
	 */
	public static final String COLUMN_STATUS = "STATUS";
	/**
	 * 数据字段：返回信息
	 */
	public static final String COLUMN_MESSAGE = "MESSAGE";
	
	/**
	 * 获取json中的值
	 * @param json
	 * @param key
	 * @return
	 */
	protected static String getJsonValue(JSONObject json,String key){
		String rst = "";
		if(json==null || !StringUtil.isNotEmpty(key)){
			return rst;
		}
		try {
			rst = json.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonValue", e.getMessage());
		}
		return rst;
	}
	
	/**
	 * 获取json中的值
	 * @param json
	 * @param key
	 * @return
	 */
	protected static int getJsonInt(JSONObject json,String key){
		int rst = 0;
		if(json==null || !StringUtil.isNotEmpty(key)){
			return rst;
		}
		try {
			rst = json.getInt(key);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonInt", e.getMessage());
		}
		return rst;
	}
	
	/**
	 * 获取json中的Json数组对象
	 * @param json
	 * @param key
	 * @return
	 */
	protected static JSONArray getJsonArray(JSONObject json,String key){
		JSONArray rst = null;
		if(json==null || !StringUtil.isNotEmpty(key)){
			return rst;
		}
		try {
			rst = json.getJSONArray(key);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonArray", e.getMessage());
		}
		return rst;
	}
	
	/**
	 * 获取json中的Json对象
	 * @param json
	 * @param key
	 * @return
	 */
	protected static JSONObject getJsonObject(JSONObject json,String key){
		JSONObject rst = null;
		if(json==null || !StringUtil.isNotEmpty(key)){
			return rst;
		}
		try {
			rst = json.getJSONObject(key);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonObject", e.getMessage());
		}
		return rst;
	}
	
	/**
	 * 获取json中的Json对象
	 * @param json
	 * @param key
	 * @return
	 */
	protected static JSONObject getJsonObject(JSONArray json,int position){
		JSONObject rst = null;
		if(json==null || position>=json.length()){
			return rst;
		}
		try {
			rst = json.getJSONObject(position);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonObject", e.getMessage());
		}
		return rst;
	}
	/**
	 * 获取json中的Json数组对象
	 * @param json
	 * @param key
	 * @return
	 */
	protected static JSONArray getJsonArray(JSONArray json,int position){
		JSONArray rst = null;
		if(json==null || position>=json.length()){
			return rst;
		}
		try {
			rst = json.getJSONArray(position);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("BaseDAO.getJsonArray", e.getMessage());
		}
		return rst;
	}
	
	@Override
	protected String doInBackground(String... params) {
		if(!StringUtil.isNotEmpty(this.url)){
			return "";
		}
		String rst = "";
		try {
			if(isMultipart){
				rst = HttpUtil.loadTextByMultipart(this.url, this.multipartParams,this.progressCallback);
			}else{
				rst = HttpUtil.loadText(this.url,this.params);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return rst;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onPostExecute(String result) {
		if(callback!=null){
			callback.callback((T)parseData(result));
		}
	}
	
	/**
	 * 数据实例化抽象方法
	 * @param jsonStr 服务器返回的json字符串
	 * @return
	 */
	protected abstract Object parseData(String jsonStr);
	
	public interface Callback<T>{
		
		public abstract void callback(T obj);
		
	}

}
